//
//  SSSRecordOrderRecordViewController.m
//  SSSMerchant
//
//  Created by 谢曦 on 2017/3/23.
//  Copyright © 2017年 xnxin. All rights reserved.
//

#import "SSSRecordOrderRecordViewController.h"
#import "SSSRecordOrderRecordCell.h"
#import "SSSRecordOrderToRecordSearchBar.h"
#import "GYZDatePicker.h"
#import "SSSRecordOrderToRecordDailyDetailViewController.h"
@interface SSSRecordOrderRecordViewController ()<GYZDatePickerDelegate>
{
    NSString *_totalPay;
    SSSRecordOrderToRecordSearchBar *_searchBar;
    NSInteger _dateButtonTag;
}
@property (nonatomic,strong)  GYZDatePicker *datePicker;
@end

@implementation SSSRecordOrderRecordViewController
CGFloat     datePickerH=250;
- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    self.isShowBottomBar=YES;
    [self.tableView registerNib:[UINib nibWithNibName:@"SSSRecordOrderRecordCell" bundle:nil
                                 ] forCellReuseIdentifier:@"SSSRecordOrderRecordCell"];
    [self configData];

    if (_isSearch==YES) {
        [self configSearchMode];
    }
    
    else{
         self.navigationItem.rightBarButtonItem=[UIBarButtonItem itemWithImage:[UIImage imageNamed:@"icon_search_history"] highImage:nil target:self action:@selector(search)];
    }
    
    
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.datePicker removeFromSuperview];
    self.datePicker=nil;
    
}
-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self datePickerHidden];
 
}

-(void)configSearchMode{
    __weak typeof(self) weakSelf = self;

   _searchBar=[[SSSRecordOrderToRecordSearchBar alloc] initWithFrame:CGRectMake(15, 64+10, SCREEN_WIDTH-30, 41)];
    _searchBar.recordSearchBarDateClick=^(NSInteger tag){
        if (tag==0) {
            //begin
        }
        if (tag==1) {
            //end
        }
        _dateButtonTag=tag;
        [weakSelf datePickerShow];
    };
    [self.view addSubview:_searchBar];

    self.tableView.transform=CGAffineTransformMakeTranslation(0, 41+10);
    
   }

-(void)configData{
    
    CGFloat totalPay=0.0;
    
    NSMutableArray *arr=@[].mutableCopy;
    for (int i=0; i<10; i++) {
        SSSRecordOrderModel *model=[SSSRecordOrderModel new];
        model.money=[NSString stringWithFormat:@"1000%d",arc4random()%20];
        model.uploadMoney=[NSString stringWithFormat:@"1000%d",arc4random()%20];
        model.date=@"2017-3-20";
        model.time=@"13:20:09";
        model.name=@"你的名字";
        model.ratio=@"20";
        model.phone=@"13320990999";
        model.type=1+arc4random()%2;
        [arr addObject:model];
      totalPay= totalPay+ [model.money floatValue];
    }
    _totalPay=[NSString stringWithFormat:@"上传支付金额 ¥%.2f",totalPay];
    self.bottomBar.backgroundColor=[UIColor colorWithHex:@"FD6E3F"];
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:_totalPay];
    
    [str addAttribute:NSFontAttributeName value:Font(15) range:NSMakeRange(0,5)];
    
    [str addAttribute:NSFontAttributeName value:Font(18) range:NSMakeRange(6, _totalPay.length-6)];
    self.bottomBarAttribute=str;
    
    self.dataArr=arr.copy;
    [self.tableView reloadData];
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SSSRecordOrderRecordCell *cell=[tableView dequeueReusableCellWithIdentifier:@"SSSRecordOrderRecordCell" forIndexPath:indexPath];
    cell.model=self.dataArr[indexPath.row];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 54;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    return [self header];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    SSSRecordOrderToRecordDailyDetailViewController *vc=[[SSSRecordOrderToRecordDailyDetailViewController alloc]init];
    vc.model=self.dataArr[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];

}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    
    [self datePickerHidden];
   
    
}

#pragma mark - DatePickerDelegate
- (void)datePicker:(GYZDatePicker *)datePicker didSelectDate:(GYZDatePickerModel *)datePickerDate {
    NSString *dateStr = [NSString stringWithFormat:@"%@-%@-%@",
                         datePickerDate.year,
                         datePickerDate.month,
                         datePickerDate.day
                         ];
    

   
    NSLog(@"%@",dateStr);
    if (_dateButtonTag==0) {
        _searchBar.dateBegin=dateStr;
    }
    if (_dateButtonTag==1) {
        _searchBar.dateEnd=dateStr;
    }

}
#pragma mark - private
-(void)datePickerShow
{
   
    
    [UIView animateWithDuration:0.5 animations:^{
        self.datePicker.transform=CGAffineTransformMakeTranslation(0, -datePickerH);
    }];
    
}
-(void)datePickerHidden
{
    [UIView animateWithDuration:0.5 animations:^{
        self.datePicker.transform=CGAffineTransformIdentity;
    }];
    
}

#pragma mark - action
-(void)search{
    SSSRecordOrderRecordViewController *searchOrderVC=[[SSSRecordOrderRecordViewController alloc] init];
    searchOrderVC.isSearch=YES;
    [self.navigationController pushViewController:searchOrderVC animated:YES];

}
-(UIView *)header{
    UIView *header=[[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 54)];
    header.backgroundColor=[UIColor clearColor];
    
    UIView *dot1=[UIView new];
    dot1.backgroundColor=ThemeGreenColor;
    dot1.layer.cornerRadius=2.5;
    [header addSubview:dot1];
    [dot1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(5, 5));
        make.centerY.equalTo(header);
        make.left.mas_equalTo(30-2.5);
    }];
    
    
    
    UILabel *date=[UILabel new];
    date.font=Font(13);
    date.text=@"支付日期";
    [date sizeToFit];
    [header addSubview:date];
    date.textColor=ThemeGrayColor;
    [date mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(dot1);
        make.left.equalTo(dot1.mas_right).with.offset(7);
    }];
    
    
    
    UILabel *money=[UILabel new];
    money.font=Font(13);
    money.text=@"支付金额";
    [money sizeToFit];
    [header addSubview:money];
    money.textColor=ThemeGrayColor;
    [money mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(dot1);
        make.right.mas_equalTo(-30);
    }];
    
    UIView *dot2=[UIView new];
    dot2.backgroundColor=ThemeGrayColor;
    dot2.layer.cornerRadius=2.5;
    [header addSubview:dot2];
    [dot2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(5, 5));
        make.centerY.equalTo(header);
        make.right.equalTo(money.mas_left).with.offset(-7);
    }];
    

    return header;

}
-(UIView *)bottomBarWithTitle{
    UIView *bar=[[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 54)];
    bar.backgroundColor=[UIColor colorWithHex:@"FD6E3F"];
    [self.view addSubview:bar ];
    
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:_totalPay];
    
    [str addAttribute:NSFontAttributeName value:Font(15) range:NSMakeRange(0,5)];
    
    [str addAttribute:NSFontAttributeName value:Font(18) range:NSMakeRange(6, _totalPay.length-6)];
    
    UILabel *attrLabel = [UILabel new] ;
    attrLabel.attributedText = str;
    [attrLabel sizeToFit];
    [bar addSubview:attrLabel];
    attrLabel.textColor=[UIColor whiteColor];
    [bar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 40));
    }];

    
    [attrLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(bar);
      
    }];
    return bar;
}
-(GYZDatePicker *)datePicker
{
    if (_datePicker==nil) {
        
        _datePicker = [[GYZDatePicker alloc]
                       initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 250)
                       delegate:self
                       datePickerStyle:GYZDatePickerStyleYearMonthDay];
        [self.view addSubview:_datePicker];
        __weak typeof(_datePicker)wdatePicker = _datePicker;
        
        _datePicker.datePickerBlock=^{
            //完成隐藏
            [UIView animateWithDuration:0.5 animations:^{
                wdatePicker.transform=CGAffineTransformMakeTranslation(0, datePickerH);
            }];
            
        };
        
    }
    return _datePicker;
}

@end
